# -*- coding: utf-8 -*-
"""
Created on 2021-6-6 09:27:34
服务端函数
@author: 王韬
"""
import datetime
import os
import pygame
from pygame.locals import *
import sys
import socket


def get_ip():
    """获取IP"""
    host = socket.gethostname()
    ip = socket.gethostbyname(host)
    # host = socket.getfqdn(socket.gethostname())
    # ip = socket.gethostbyname(host)
    return ip


def get_time():
    """获取发送时间"""
    now = datetime.datetime.now()
    send_time = now.strftime("%Y-%m-%d %H:%M:%S")
    return send_time


def send_msg(data, sock, **kwargs):
    """send data to client

    Arguments:
        data -- data to be send
        sock -- socket of target client
    Keyword arguments:
        info -- information about this send
    """
    if 'info' in kwargs:
        info = kwargs['info']
    else:
        info = "NO info"
    try:
        send_data = str(data).encode('utf-8')
        sock.send(send_data)
        print('send:', send_data, 'to', sock.getpeername(), info)
    except:
        print('send fail:', data, 'to', sock.getpeername(), info)
